package view;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class DBConnector {
	// PRIVATE 
	  private static boolean credentialsOk = false;
	  private static String machinename = "localhost";
	  private static String dbname = "librarydb";
	  private static String username = "libtester";
	  private static String passwd = "secret";
	  private static final String encoding = "UTF-8";
	
	public static Connection DBConnect() {
		if (!credentialsOk) DBConnector.readConfig();
		Connection con = null;
		try {
			System.out.println("connecting...");
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://"+machinename+":3306/"+dbname, username, passwd); // null
																			// password
			if (!con.isClosed())
				System.out.println("Sucessfully connected.");
			else
				System.out.println("connection closed");	

		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
			try {
				con.close();
			} catch (SQLException esql) {
			}
		} 
		return con;
	}
	
	 /** Write fixed content to the given file. */
	 private static void write(String SQLmachine,String SQLuser, String SQLpass,String LibDBname ) throws IOException  {
	    //log("Writing to file named . Encoding: " + fEncoding);
	    Writer out = new OutputStreamWriter(new FileOutputStream("jalibconf.txt"), encoding);
	    try {
	    	dbname=LibDBname;
	    	username=SQLuser;
	    	passwd=SQLpass;
	    	machinename=SQLmachine;
	    	credentialsOk = true;
	    	out.write(SQLmachine+"\r\n");
	      out.write(SQLuser+"\r\n");
	      out.write(SQLpass+"\r\n");
	      out.write(LibDBname);
	      
	    }
	    finally {
	      out.close();
	    }
	  }
	 
	  private static void read() throws IOException {
		    String line;
		    int linectr=0;
		    Scanner scanner = new Scanner(new FileInputStream("jalibconf.txt"), encoding);
		    System.out.println("will try to read.");
		    try {
		      while (scanner.hasNextLine()){
		    	  line = scanner.nextLine();
		    	  switch (linectr) {
		    	  case 0:machinename = line;
		    		  break;
		    	  case 1:username = line;
	    		  break;
		    	  case 2:passwd = line;
	    		  break;
		    	  case 3: dbname= line;
	    		  break;
		    	  }
		    	  linectr++;
		      }
		      System.out.println("read lines:" +linectr);
		      credentialsOk = true;
		    }
		    finally{
		      scanner.close();
		    }
		  }
	  
	  public static void readConfig() {
		  try {
			  System.out.println("Reading config...");
		  read();
		  }
		  catch (Exception e1){
			  try {
				  System.out.println("Creating config...");
				  write(machinename, username, passwd, dbname);
			  }
			  catch(Exception e2) {
				  
			  }
		  }
	  }
	  
	  public static void writeConfig(String machine, String name, String pass, String db) {
			  try {
				  write(machine,name, pass, db);
			  }
			  catch(Exception e2) {
				  
			  }
	  }
	  
	  public static void writeSQLScript()  throws IOException {
		    String line;
		    int linectr=0;
		    Scanner scanner = new Scanner(new FileInputStream("CREATEDB.SQL"), encoding);
		    Writer out = new OutputStreamWriter(new FileOutputStream("jalibCreateDB.SQL"), encoding);
		    try {
		      while (scanner.hasNextLine()){
		    	  line = scanner.nextLine();
		    	  out.write(line.replaceAll("librarydb", dbname)+"\r\n");
		    	  linectr++;
		      }
		      System.out.println("read lines:" +linectr);
		      credentialsOk = true;
		    }
		    finally{
		      scanner.close();
		      out.close();
		    }
	  }
	  
	  public static void writeCreateDBBatchfile()  throws IOException {
		    Writer out = new OutputStreamWriter(new FileOutputStream("CreateJADB.bat"), encoding);
		    try {
		    	  out.write("\"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysql.exe\" \"--user="+username+"\" \"--password="+passwd+"\"   < \"jalibCreateDB.SQL\""+"\r\n");
		    }
		    finally{
		      out.close();
		    }
	  }
}
